package com.qike.duola.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qike.duola.domain.*;
import com.qike.duola.dto.ApprovalListDto;
import com.qike.duola.dto.ApprovalOperationDto;
import com.qike.duola.dto.ApprovalProcessSetDto;
import com.qike.duola.dto.WaitApprovalListDto;
import com.qike.duola.enums.*;
import com.qike.duola.mapper.*;
import com.qike.duola.service.ApprovalService;
import com.qike.duola.service.IndexService;
import com.qike.duola.utils.TimeTool;
import com.qike.duola.vo.*;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Eric.zhou
 * @Date: 2020/9/27 11:52
 */
@Service
public class IndexServiceImpl implements IndexService {


    @Autowired
    private DuolaTransOrderMapper transOrderMapper;
    @Autowired
    private DuolaContractMapper contractMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private DuolaTrainMapper duolaTrainMapper;


    @Override
    public IndexVo index() {
        Double sendTotal = new Double(0);
        Double distanceTtoal = new Double(0);
        Double saleTotal = new Double(0);
        DuolaTransOrderExample transOrderExample = new DuolaTransOrderExample();
        transOrderExample.createCriteria().andOrderStatusNotEqualTo(TransOrderStatusEnums.WAIT_LOAD.getEcode());
        List<DuolaTransOrder> transOrders = transOrderMapper.selectByExample(transOrderExample);
        for (DuolaTransOrder transOrder : transOrders) {
            sendTotal=sendTotal+transOrder.getTotalVolume();
            distanceTtoal=distanceTtoal+transOrder.getDistance();
        }
        IndexVo vo = new IndexVo();
        //培训活动 获取所有未结束的培训 按照创建时间排序
        DuolaTrainExample duolaTrainExample = new DuolaTrainExample();
        duolaTrainExample.createCriteria().andStatusEqualTo(StatusEnums.NO.getEcode()).andStartTimeGreaterThanOrEqualTo(LocalDateTime.now());
        duolaTrainExample.setOrderByClause("add_time desc");
        List<DuolaTrain> duolaTrains = duolaTrainMapper.selectByExample(duolaTrainExample);
        List<ActivityVo> activityList = new ArrayList<>();
        for (DuolaTrain duolaTrain : duolaTrains) {
            ActivityVo activityVo = new ActivityVo();
            activityVo.setImage(duolaTrain.getPhoto());
            activityVo.setId(duolaTrain.getId());
            activityList.add(activityVo);
        }
        vo.setActivityList(activityList);
        vo.setDistanceTotal(distanceTtoal);
        //销售额度
        List<DuolaContract> duolaContracts = contractMapper.selectByExample(new DuolaContractExample());
        for (DuolaContract duolaContract : duolaContracts) {
            if (duolaContract.getTotalPrice()!=null){
                saleTotal+=duolaContract.getTotalPrice().doubleValue();
            }
        }
        vo.setSaleTotal(saleTotal);
        //发货总量
        vo.setSendTotal(sendTotal);
        List<SysUser> sysUsers = sysUserMapper.selectByExample(new SysUserExample());
        vo.setPersonNum(sysUsers.size());
        return vo;
    }
}
